package com.IntelligenceInsight.Myth.service;


import com.IntelligenceInsight.Myth.mapper.JobMapper;
import com.IntelligenceInsight.Myth.model.Job;
import com.IntelligenceInsight.Myth.util.NoteRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class JobService {
    @Autowired
    private JobMapper mapper;

    public NoteRequest JobListAll(int page, int limit) {
        NoteRequest res = new NoteRequest();
        List<Job> job = mapper.JobListAll(page, limit);
        if(job == null) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success");
            res.setData(job);
        }
        return res;
    }

    public int JobListAllCount() {
       int count = mapper.JobListAllCount();
        return count;
    }
    public NoteRequest JobListByOwner(String owner) {
        NoteRequest res = new NoteRequest();
        List<Job> job = mapper.JobListByOwner(owner);
        if(job == null) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success");
            res.setData(job);
        }
        return res;
    }

    public NoteRequest JobByUuid(String uuid) {
        NoteRequest res = new NoteRequest();
        Job job = mapper.JobByUuid(uuid);
        if(job == null) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success");
            res.setData(job);
        }
        return res;
    }

    public NoteRequest JobByName(String name) {
        NoteRequest res = new NoteRequest();
        Job job = mapper.JobByName(name);
        if(job == null) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success");
            res.setData(job);
        }
        return res;
    }


    public NoteRequest JobByModel(String model) {
        NoteRequest res = new NoteRequest();
        List<Job> job = mapper.JobByModel(model);
        if(job == null) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success");
            res.setData(job);
        }
        return res;
    }


    public NoteRequest JobAdd(String model, String dataset, String params) {
        NoteRequest res = new NoteRequest();
        int n = mapper.JobAdd(model, dataset, params);
        if(n < 1) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success: ");
        }
        return res;
    }

//    public NoteRequest JobAdd(String uuid, String name, String model, String owner, String parameters, String resultPath) {
//        NoteRequest res = new NoteRequest();
//        int n = mapper.JobAdd(uuid, name, model, owner, parameters, resultPath);
//        if(n < 1) {
//            res.setStatus(1);
//            res.setMsg("failure");
//        } else {
//            res.setStatus(0);
//            res.setMsg("success: " + uuid);
//        }
//        return res;
//    }

    public NoteRequest JobStateChange(String uuid, String state, String result) {
        NoteRequest res = new NoteRequest();
        int n = mapper.JobStateChange(uuid, state, result);
        if(n < 1) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success: " + uuid);
        }
        return res;
    }

    public NoteRequest JobDelete(String uuid) {
        NoteRequest res = new NoteRequest();
        int n = mapper.JobDelete(uuid);
        if(n < 1) {
            res.setStatus(1);
            res.setMsg("failure");
        } else {
            res.setStatus(0);
            res.setMsg("success: " + uuid);
        }
        return res;
    }

}
